﻿@page "/PDPage"
@inject CKDServer cKDServer
@using System.ComponentModel
@using AntDesign.TableModels
@inject ModalService _modalService
@inject MessageService _message
@using Microsoft.AspNetCore.Components

<h4>盘点</h4>
<div>
    <Row>
        <Col Span="24">请选择盘点作业表</Col>
    </Row>
    <Row>
        <Col Span="24">
        <Select style="width:100%" DataSource="@DataList"
                AllowClear="true"
                ValueOnClear="@("")"
                @bind-Value="@selet"
                ValueName="@nameof(PDBlist.FBILLNO)"
                LabelName="@nameof(PDBlist.Remark)">
        </Select>
        </Col>
    </Row>

    <Row>
        <Col Span="24">扫码输入框</Col>
    </Row>
    <Row>
        <Col Span="24">
        <input @ref="input" Type="text" style="width:100%;" placeholder="Basic usage" @bind:event="oninput" @bind="@ss" @onkeydown="Change" />
 </Col>
    </Row>
    <Row>
        <Col Span="24">批号</Col>
    </Row>
    <Row>
        <Col Span="24">
        <Input style="width:100%" Placeholder="Basic usage" @bind-Value="@Batch"  />
        </Col>
    </Row>
    <Row>
        <Col Span="24">数量</Col>
    </Row>
    <Row>
        <Col Span="24">
        <Input style="width:100%" Type="number" Placeholder="Basic usage" @bind-Value="@sum" />
        </Col>
    </Row>

    @*<br>
    <label>以下数据只有在盘盈增加时填写</label>*@
    <Row>
        <Col Span="24">物料编码</Col>
    </Row>
    <Row>
        <Col Span="24">
        <Input style="width:100%" Type="wlbm" Placeholder="Basic usage" @bind-Value="@wlbm" />
        </Col>
    </Row>
    <br>

    @* <Row>
    <Col Span="24">生产日期</Col>
    </Row>
    <Row>
    <Col Span="24">
    <DatePicker @bind-Value="@dateTime" Picker="@DatePickerType.Date" style="width:100%" />
    </Col>
    </Row>*@
    <Row>
        <div>
            <Checkbox @bind-Checked="isChecked" Style="zoom:150%;align-content:center" />
            <label style="font-size:large;align-content:center">增加盘点录入</label>

        </div>
    </Row>

    <Row>
        <Col Span="24">
            @* <Button style="width:100%" Type="@ButtonType.Primary" >解析二维码</Button>*@

      @*  <Button style="width:100%" Type="@ButtonType.Primary" @onclick="@Creat">盘点录入</Button>
        <br>
        <br>
        <Button style="width:100%" Type="@ButtonType.Primary" @onclick="@Creat3">盘盈增加录入</Button>
     *@ 
        <Button style="width:100%" Type="@ButtonType.Primary" @onclick="@Creat2">提交</Button>
        </Col>
    </Row>
</div>


@code {
    Checkbox checkbox;
    string Batch { get; set; }
    float sum { get; set; }
    string wlbm { get; set; }
    DateTime dateTime { get; set; } = DateTime.Now.Date;
    bool Flag = false;
    ObservableCollection<PDBlist> DataList { get; set; } = new ObservableCollection<PDBlist>();
    string selet;
    string ss { get; set; }
    private ElementReference input;
    //private System.ComponentModel.Component input;
    //private Input<string> input;
    bool isChecked = false;
    List<Task> tasks = new List<Task>();
    
    private async Task Change(KeyboardEventArgs e)
    {
        
        try
        {
            if (e.Key == "Enter")
            {   

                //await Task.Delay(100);
                if (!string.IsNullOrEmpty(ss))
                {

                    var a = ss.Split(',');
                    Batch = a[0];
                    wlbm = a[1];
                    sum = Convert.ToSingle(a[2]);
                }
                if (isChecked == false)
                {
                    string key = $"updatable-{DateTime.Now.Ticks}";
                    var config = new MessageConfig()
                        {
                            Content = "更新中...",
                            Key = key
                        };
                    _message.Loading(config);
                    string result = await Creat();
                    config.Content = result;
                    config.Duration =1;
                    await _message.Success(config);
                }
                else
                {
                    string key = $"updatable-{DateTime.Now.Ticks}";
                    var config = new MessageConfig()
                        {
                            Content = "更新中...",
                            Key = key
                        };
                    _message.Loading(config);
                    string result = await Creat3();
                    config.Content = result;
                    config.Duration =1;
                    await _message.Success(config);
                }
            }


        }
        catch (Exception)
        {


        }


    }
    protected override async Task OnInitializedAsync()
    {
        var lists = await cKDServer.GetPDBS();
        DataList.Clear();
        foreach (var item in lists)
        {
            DataList.Add(item);
        }
        base.OnInitialized();
    }
    private async Task init()
    {

        var lists = await cKDServer.GetPDBS();
        DataList.Clear();
        if (lists != null)
        {
            foreach (var item in lists)
            {
                DataList.Add(item);
            }
            this.StateHasChanged();
        }

    }

    private async Task<string> Creat()
    {
        #region
        //await _modalService.ConfirmAsync(new ConfirmOptions()
        //    {
        //        Title = $"提示-{selet}-{Batch}-{sum}",
        //        Icon =@<Icon Type="exclamation-circle" Theme="Outline"></Icon>,
        //    OkText = "确认",
        //    OnOk = async (e) =>
        //    {
        //        try
        //        {
        //            if (Batch == null || Batch.Trim().Length == 0)
        //            {
        //                _message.Info("请输入批号");
        //                return;
        //            }
        //            var aa = await cKDServer.UpdataPdb(selet, Batch, sum);
        //            if (aa == "OK")
        //            {

        //                dateTime = DateTime.Now.Date;
        //                this.StateHasChanged();

        //            }
        //            _message.Info(aa);
        //        }
        //        catch (Exception)
        //        {

        //            _message.Info("调用接口失败");
        //        }
        //    },
        //    CancelText = "取消",
        //    Content = "是否提交？",
        //});
        #endregion
        try
        {
            if (Batch == null || Batch.Trim().Length == 0)
            {
                ss = "";
                Batch = "";
                sum = 0;
                wlbm = "";
                //_message.Info("请输入批号");
                return "请输入批号";
            }
            var aa = await cKDServer.UpdataPdb(selet, Batch, sum);
            if (aa == "OK")
            {

                dateTime = DateTime.Now.Date;
                this.StateHasChanged();
            }
            ss = "";
            Batch = "";
            sum = 0;
            wlbm = "";
            //_message.Info(aa);
            return aa;
        }
        catch (Exception)
        {

            //_message.Info("调用接口失败");
            return "调用接口失败";
        }



    }

    private async Task Creat2()
    {
        await _modalService.ConfirmAsync(new ConfirmOptions()
            {
                Title = $"提示-{selet}-{Batch}-{sum}",
                Icon =@<Icon Type="exclamation-circle" Theme="Outline"></Icon>,
            OkText = "确认",
            OnOk = async (e) =>
            {
                var aa = await cKDServer.submitPD(selet);

                _message.Info(aa);
            },
            CancelText = "取消",
            Content = "是否提交？",
        });

        ss = "";
        Batch = "";
        sum = 0;
        wlbm = "";
    }
    private async Task<string> Creat3()
    {

        try
        {
            #region 修改前
            //if (wlbm == null || wlbm.Trim().Length == 0)
            //{
            //    _message.Info("请输入物料编码");
            //    return "请输入物料编码";
            //}
            //if (Batch == null || Batch.Trim().Length == 0)
            //{
            //    _message.Info("请输入批号");
            //    return;
            //}

            //var aa = await cKDServer.UpdataADDPdb(selet, wlbm, Batch, sum);
            //if (aa == "OK")
            //{

            //    dateTime = DateTime.Now.Date;
            //    this.StateHasChanged();

            //}
            //_message.Info(aa);
            //ss = "";
            //Batch = "";
            //sum = 0;
            //wlbm = "";
            #endregion
            if (wlbm == null || wlbm.Trim().Length == 0)
            {
                ss = "";
                Batch = "";
                sum = 0;
                wlbm = "";
                return "请输入物料编码";
            }
            if (Batch == null || Batch.Trim().Length == 0)
            {
                ss = "";
                Batch = "";
                sum = 0;
                wlbm = "";
                return "请输入批号";
            }

            var aa = await cKDServer.UpdataADDPdb(selet, wlbm, Batch, sum);
            if (aa == "OK")
            {

                dateTime = DateTime.Now.Date;
                this.StateHasChanged();

            }

            ss = "";
            Batch = "";
            sum = 0;
            wlbm = "";
            return aa;
        }
        catch (Exception)
        {

            return "调用接口异常";
        }


    }
}
